package cn.hh.wechatkit.api.card;

import cn.hh.wechatkit.data.postdata.card.Meta_CardCheckTicketStatePostData;
import cn.hh.wechatkit.data.returndata.card.Meta_RData_CardState;
import cn.hh.wechatkit.exception.Wx_Exception_BadNetwork;
import cn.hh.wechatkit.exception.Wx_Exception_InvalidJsonParse;
import cn.hh.wechatkit.exception.Wx_Exception_InvalidResponse;
import cn.hhchat.kit.http.httpUtil;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import lombok.extern.slf4j.Slf4j;
import okhttp3.Response;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.io.IOException;

import static cn.hh.wechatkit.helper.WxConst.CancelCard_Post_URL;
import static cn.hh.wechatkit.helper.WxConst.CancelTicket_Post_URL;
import static cn.hh.wechatkit.helper.WxConst.CheckTicketState_Post_URL;

/**
 * Created this one by HMH on 2017/8/19.
 */
@Slf4j
public class WxAPI_Card_CancelCard_API {
    //日志
    /**
     * 令卡券失效 API
     * 删除卡券接口
     * @param cardId 卡券Id
     * @param ACCESS_TOKEN 基础Token
     * @return 卡券状态消息
     */
    public static Meta_RData_CardState cancelCard(String cardId, String ACCESS_TOKEN)  {
        String url = CancelCard_Post_URL.replace("ACCESS_TOKEN", ACCESS_TOKEN);
        JSONObject jsonObject = new JSONObject();
        jsonObject.put("card_id", cardId);
        Response response = null;
        try {
            response = httpUtil.postJson(url, jsonObject.toJSONString());
        } catch (IOException e) {
            log.error(" ==> 网络请求失败", e);
            throw new Wx_Exception_BadNetwork("网络请求失败", e);
        }
        //获取数据
        if (response != null) {
            String body;
            try {
                body = response.body().string();
                return JSON.parseObject(body, Meta_RData_CardState.class);
            } catch (IOException e) {
                log.error(" ==> json转换失败", e);
                throw new Wx_Exception_InvalidJsonParse("json转换失败");
            }
        }
        throw new Wx_Exception_InvalidResponse("没有收到响应，或者请求出错");
    }

    //日志

    /**
     * 令票券失效的api
     * @param code 票券的code
     * @param reason 失效原因
     * @param ACCESS_TOKEN 基础token
     * @return 响应结果
     */
    public static Meta_RData_CardState cancelTicket(String code, String reason, String ACCESS_TOKEN)  {
        String url = CancelTicket_Post_URL.replace("ACCESS_TOKEN", ACCESS_TOKEN);
        JSONObject jsonObject = new JSONObject();
        jsonObject.put("code", code);
        jsonObject.put("reason", reason);
        Response response = null;
        try {
            response = httpUtil.postJson(url, jsonObject.toJSONString());
        } catch (IOException e) {
            log.error(" ==> 网络请求失败", e);
            throw new Wx_Exception_BadNetwork("网络请求失败", e);
        }
        //获取数据
        if (response != null) {
            String body;
            try {
                body = response.body().string();
                return JSON.parseObject(body, Meta_RData_CardState.class);
            } catch (IOException e) {
                log.error(" ==> json转换失败", e);
                throw new Wx_Exception_InvalidJsonParse("json转换失败");
            }
        }
        throw new Wx_Exception_InvalidResponse("没有收到响应，或者请求出错");
    }
}
